//
//  XYCommon.swift
//  XYReadBook
//
//  Created by fox on 2017/9/4.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit
/*
 刀哥的那个获取屏幕宽高,有点太恶心了.还得方法调用
 */
let changeSoureNotificationKey = "changeSoureNotificationKey" /// 更改源目录的Key
/// 屏幕宽度
let ScreenWidth:CGFloat = UIScreen.main.bounds.size.width

/// 屏幕高度
let ScreenHeight:CGFloat = UIScreen.main.bounds.size.height

/// 屏幕分辨率
let Scale:CGFloat = UIScreen.main.scale


func XYLog<T>(messsage : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {
   
    #if DEBUG
        
        let fileName = (file as NSString).lastPathComponent
        
        print("\(fileName):(\(lineNum))-\(messsage)")
        
    #endif
}

func getCurrentStringTime() -> String
{
    let currentDate = Date()
    //设置时间显示样式
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale.current //设置时区，时间为当前系统时间
    //输出样式
    dateFormatter.dateFormat = "yyyy-MM-dd  HH:mm:ss"
    let stringDate = dateFormatter.string(from: currentDate)
    return stringDate
}
typealias Task = (_ cancel : Bool) -> Void

func delay(_ time: TimeInterval, task: @escaping ()->()) ->  Task? {

    func dispatch_later(block: @escaping ()->()) {
        let t = DispatchTime.now() + time
        DispatchQueue.main.asyncAfter(deadline: t, execute: block)
    }
    var closure: (()->Void)? = task
    var result: Task?

    let delayedClosure: Task = {
        cancel in
        if let internalClosure = closure {
            if (cancel == false) {
                DispatchQueue.main.async(execute: internalClosure)
            }
        }
        closure = nil
        result = nil
    }

    result = delayedClosure

    dispatch_later {
        if let delayedClosure = result {
            delayedClosure(false)
        }
    }
    return result
}

func cancel(_ task: Task?) {
    task?(true)
}


